﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AMOOF {
    public class DataImport {

        public System.IO.FileInfo openFile() {
            OpenFileDialog f = new OpenFileDialog();
            f.Filter = "Data Files (*.csv)|*.csv";
            f.Title = "Data Importer";
            DialogResult r = f.ShowDialog();
            if (r == DialogResult.OK) {
                return new System.IO.FileInfo(f.FileName);
            }
            else
                return null;
        }


        public uint GetLineCount(System.IO.FileInfo file) {
            uint count = 0;
            System.IO.StreamReader r = new System.IO.StreamReader(file.FullName);
            while (r.ReadLine() != null) { count++; }
            r.Close();
            return count;
        }

        /// <summary>
        /// Imports the data from a csv file and places it into a string array within a list.
        /// This version of importData needs to be handled very carefully, and must be used 
        /// within a foreach loop. This is because it is the most-efficient way we have to do constant 
        /// progress updates.
        /// </summary>
        /// <param name="file">This holds all the information about the file.</param>
        /// <returns>A List of sample data represented as a string array.</returns>
        public IEnumerable<object> importData(System.IO.FileInfo file) {
            System.IO.StreamReader r = new System.IO.StreamReader(file.FullName);
            List<string[]> dataStore = new List<string[]>();
            Dictionary<double, int> resultsAsKVP = new Dictionary<double, int>();
            uint count = 1;
            string line;
            string[] row;

            yield return String.Format("Importing Data from {0}... ", file.Name);

            while ((line = r.ReadLine()) != null) {
                yield return count++;
                row = line.Split(',');
                List<string> cleanRow = new List<string>();
                for (int i = 0; i < row.Length; i++) {

                    if (!row[i].Equals("")) {
                        cleanRow.Add(row[i]);
                    }
                }
                dataStore.Add(cleanRow.ToArray());
            }

            r.Close();

            yield return String.Format("Cleaning imported data from {0}... ", file.Name);
            count = 1;

            foreach (string[] strings in dataStore) {

                yield return count++;
                double key;
                int value = 1;
                if (!(strings.Length > 1 && int.TryParse(strings[1], out value))) {
                    value = 1;
                }

                if (double.TryParse(strings[0], out key)) {

                    int tValue;
                    if (resultsAsKVP.TryGetValue(key, out tValue)) {
                        resultsAsKVP[key] += 1;
                    }
                    else {
                        resultsAsKVP.Add(key, value);
                    }
                }
            }

            yield return resultsAsKVP;
        }

        public Dictionary<double, int> importData(System.IO.FileInfo file, int c) {
            System.IO.StreamReader r = new System.IO.StreamReader(file.FullName);
            List<string[]> dataStore = new List<string[]>();
            Dictionary<double, int> resultsAsKVP = new Dictionary<double, int>();
            string line;
            string[] row;

            while ((line = r.ReadLine()) != null) {

                row = line.Split(',');
                List<string> cleanRow = new List<string>();
                for (int i = 0; i < row.Length; i++) {

                    if (!row[i].Equals("")) {
                        cleanRow.Add(row[i]);
                    }
                }
                dataStore.Add(cleanRow.ToArray());
            }

            r.Close();

            foreach (string[] strings in dataStore) {

                double key;
                int value = 1;
                if ((strings.Length > 1 && int.TryParse(strings[1], out value))) {
                    value = 1;
                }

                if (double.TryParse(strings[0], out key)) {

                    int tValue;
                    if (resultsAsKVP.TryGetValue(key, out tValue)) {
                        resultsAsKVP[key] += 1;
                    }
                    else {
                        resultsAsKVP.Add(key, value);
                    }
                }
            }

            return resultsAsKVP;
        }
    }
}
